package com.shipwe.loans.ui.presenter

import com.shipwe.language.LocaleContract
import com.shipwe.language.ShipweLanguages
import com.shipwe.loans.configs.ShipweConfigs
import com.shipwe.loans.http.ApiService
import com.shipwe.loans.http.IHttpCallback
import com.shipwe.loans.pro.ShipwePresenter
import com.shipwe.loans.ui.view.IAuthStepsView
import com.shipwe.loans.utils.helper.CustomProcessHelper
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/19 11:59
 * @Annotation：进件 Presenter 实现
 */
class AuthStepsPresenter(private val iView: IAuthStepsView): ShipwePresenter(iView) {

    /**
     * 当前步骤的名称
     */
    private var eventStepTitle = ""
    fun getAuthStepEventTitle(): String{
        return eventStepTitle
    }

    /**
     * 根据Step类型显示不同的Page
     */
    fun loadAuthStepPageAction(){
        //移除所有步骤
        iView.removeAuthStepsAllViews()

        val allStepTitles = CustomProcessHelper.getAuthStepTitleList()
        //当前进行的步骤
        val curtIndex = CustomProcessHelper.getAuthStepIndex()
        for (i in 0 until allStepTitles.size) {
            val showStepTitle = "${(i+1)}.${allStepTitles[i]}"
            if (i < curtIndex){//已完成
                iView.showAuthStepAfterView(showStepTitle)
            } else if (i == curtIndex){//正在进行

            } else {//未开始
                iView.showAuthStepBeforeView(showStepTitle)
            }
        }

        val allSteps = CustomProcessHelper.getAuthAllStepList()
        if (curtIndex == allSteps.size){//所有步骤已完成
            iView.showAuthStepsCompleteViews()
            return
        }
        val processData = CustomProcessHelper.getAuthProcessData()
        eventStepTitle = allSteps[curtIndex]
        var stepTitle = allStepTitles[curtIndex]
        //是否是坦桑地区,需要根据语言显示
        if (ShipweConfigs.isTanz()){
            val stepTitleItem = JSONObject(stepTitle)
            val curtLocale = ShipweLanguages.getAppLanguage(iView.getAct())
            stepTitle = when(curtLocale.language){
                LocaleContract.getSwahiliLocale().language -> {//坦桑
                    stepTitleItem.optString("sw")
                }
                else -> {
                    stepTitleItem.optString("en")
                }
            }
        }
        stepTitle = "${curtIndex+1}.$stepTitle"
        //获取对应的key
        val curtKey = allSteps[curtIndex]
        //Contact 类型
        if (processData!!.has(curtKey)){
            val stepData = processData.optJSONObject(curtKey)
            stepData?.put("showTitle",stepTitle)
            stepData?.put("stepTitle",eventStepTitle)
            if (stepData != null && stepData.has("isContact") && stepData.optString("isContact") == "YES"){
                iView.showAuthStepContactView(stepData)
                return
            }
        }

        //todo Ocr证件照样式
        if (curtKey.contains("ocr_info")){
            val stepData = JSONObject()
            stepData.put("showTitle",stepTitle)
            stepData.put("stepTitle",eventStepTitle)
            iView.showAuthStepOcrView(stepData)
            return
        }

        //todo Selfie自拍照样式
        if (curtKey.contains("sefie_info")){
            val stepData = JSONObject()
            stepData.put("showTitle",stepTitle)
            stepData.put("stepTitle",eventStepTitle)
            iView.showAuthStepSelfieView(stepData)
            return
        }

        //todo Account账户样式（法语地区绑定账号）
        if (curtKey.contains("account_info") && ShipweConfigs.isFrench()){
            val stepData = JSONObject()
            stepData.put("showTitle",stepTitle)
            stepData.put("stepTitle",eventStepTitle)
            iView.showAuthStepAccountView(stepData)
            return
        }
        //常规通用样式
        if (processData.has(curtKey)){
            val stepData = processData.optJSONObject(curtKey)
            if (stepData != null) {
                stepData.put("showTitle",stepTitle)
                stepData.put("stepTitle",eventStepTitle)
                iView.showAuthStepCommonView(stepData)
            }
        }
    }

    /**
     * 提交数据
     */
    fun submitAuthStepsProcessAction(params: JSONObject){
        iView.showShipweLoading()
        val httpUrl = ApiService.getHttpUrl()
        doHttpPostAction(httpUrl, params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.showAuthStepsSubmitSuccess()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.dismissShipweLoading()
            }
        })
    }
}